package top.xzxsrq.datahelp.zhixin.module.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xzxsrq.datahelp.zhixin.config.FileConfig;
import top.xzxsrq.datahelp.zhixin.module.file.entity.FileTable;
import top.xzxsrq.datahelp.zhixin.module.file.service.FileService;
import top.xzxsrq.datahelp.zhixin.utils.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: datahelp
 * @create: 2021-12-09 15:37
 **/
@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("saveTable")
    public AjaxResult saveTable(@RequestBody FileTable fileTable, HttpServletRequest request) throws IOException {
        fileService.writeFile(fileTable, request);
        return AjaxResult.success();
    }

    @GetMapping("getFileList")
    public AjaxResult getFileList() throws IOException {
        List<String> strings = fileService.readFileMap()
                .keySet()
                .stream()
                .map(i-> i.replace("." + FileConfig.SUFFIX,""))
                .collect(Collectors.toList());
        return AjaxResult.success(strings);
    }

    @GetMapping("delFile")
    public AjaxResult delFile(String name){
        fileService.delFile(name);
        return AjaxResult.success();
    }
}
